/**
*tagfolder
*Licensed under LGPL
*http://code.google.com/p/tagfolder
*chuanshenglu@gmail.com
*xrjieyee@gmail.com
*
*/

#ifndef __TF_SERVER_TASK_H__
#define __TF_SERVER_TASK_H__

#include "../common/include/TFChannel.h"
#include <map>

class TFServerTaskDispatcher;
class TFServerTaskHandler;
//=======================================
//Dispatch Server End Message;
//=======================================
class TFServerTaskDispatcher
{
public:
	static TFServerTaskDispatcher * GetInstance( );
	void Dispatch ( TFMessage& , TFDuplexChannel& );
	bool AddHandler ( long ,TFServerTaskHandler* ) ;
	bool RemoveHandler( long ) ;
	TFServerTaskHandler* GetHandler( long );
	virtual ~TFServerTaskDispatcher();
private:
	TFServerTaskDispatcher( ) ;
	std::map < long , TFServerTaskHandler* > TaskHandlers;
	static TFServerTaskDispatcher * instance;
};

//=======================================
//Handle Server End Message;
//=======================================
typedef void ( *TFServerHook ) ( TFMessage & , TFDuplexChannel & );
class TFServerTaskHandler
{
public:
	TFServerTaskHandler()
		:hook(NULL)
	{
	}
	TFServerTaskHandler(TFServerHook _hook)
		:hook(_hook)
	{
	}
	~TFServerTaskHandler()
	{
	}
	virtual void operator ( ) ( TFMessage & msg , TFDuplexChannel & channel )
	{
		if(hook!=NULL){
			hook ( msg , channel );
		}
	}
public:
	virtual void SetHook(TFServerHook _hook)
	{
		hook=_hook;
	}
	virtual void SetHandler(TFServerHook handler)
	{
		SetHook(handler);
	}
private:
	TFServerHook hook;
};

//=================================================================
//Predefined task handler
//=================================================================
//On Add Tag;
class TFSTaskOnAddTag
	:public TFServerTaskHandler
{
public:
	virtual void operator ( ) (TFMessage &,TFDuplexChannel & );
};
//On Add File
class TFSTaskOnAddFile
	:public TFServerTaskHandler
{
public:
	virtual void operator ( ) ( TFMessage &, TFDuplexChannel &);
};

//On Link Tag to File
class TFSTaskOnLinkTagToFile
	:public TFServerTaskHandler
{
public:
	virtual void operator ( ) ( TFMessage & , TFDuplexChannel & );
};
//Get all tags
class TFSTaskOnGetAllTags
	:public TFServerTaskHandler
{
public:
	virtual void operator ( ) ( TFMessage & , TFDuplexChannel & );
};
//Get all files
class TFSTaskOnGetAllFiles
	:public TFServerTaskHandler
{
public:
	virtual void operator ( ) ( TFMessage &, TFDuplexChannel & );
};
//Get all tags  belonging to one file
class TFSTaskOnGetTagsOfFile
	:public TFServerTaskHandler
{
public:
	virtual void operator ( ) ( TFMessage &, TFDuplexChannel & );
};
//Get all files have this tag;
class TFSTaskOnGetFilesWithTag
	:public TFServerTaskHandler
{
public:
	virtual void operator ( ) ( TFMessage &, TFDuplexChannel & );
};

///On Terminate server
class  TFSTaskOnTeminateServer
	:public TFServerTaskHandler
{
public:
	virtual void operator ( ) ( TFMessage &, TFDuplexChannel & );
};

#endif
